package com.bbu.javaexam.controller;

import com.bbu.javaexam.domain.PaperManage;
import com.bbu.javaexam.domain.RespData;
import com.bbu.javaexam.service.JudgeQuestionService;
import com.bbu.javaexam.service.MultiQuestionService;
import com.bbu.javaexam.service.PaperService;
import com.bbu.javaexam.domain.vo.Item;
import com.bbu.javaexam.service.FillQuestionService;
import com.bbu.javaexam.util.RespDataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName ItemController.java
 * @Description TODO
 */
@RestController
public class ItemController {
    @Autowired
    MultiQuestionService multiQuestionService;
    @Autowired
    FillQuestionService fillQuestionService;
    @Autowired
    JudgeQuestionService judgeQuestionService;
    @Autowired
    PaperService paperService;

    @PostMapping("/item")
    public RespData ItemController(@RequestBody Item item) {
        // 选择题数
        Integer changeNumber = item.getChangeNumber();
        // 填空题数
        Integer fillNumber = item.getFillNumber();
        // 判断题数
        Integer judgeNumber = item.getJudgeNumber();
        //试卷id
        Integer paperId = item.getPaperId();
        //从数据库随机获取选择题
        List<Integer> changeNumbers = multiQuestionService.findBySubject(item.getSubject(), changeNumber);
        if (changeNumbers == null) {
            return RespDataHandler.buildRespData(400, "选择题数据库获取失败", null);
        }
        for (Integer number : changeNumbers) {
            PaperManage paperManage = new PaperManage(paperId, 1, number);
            int index = paperService.add(paperManage);
            if (index == 0)
                return RespDataHandler.buildRespData(400, "选择题组卷保存失败", null);
        }
        //从数据库随机获取填空题
        List<Integer> fills = fillQuestionService.findBySubject(item.getSubject(), fillNumber);
        if (fills == null)
            return RespDataHandler.buildRespData(400, "填空题数据库获取失败", null);
        for (Integer fillNum : fills) {
            PaperManage paperManage = new PaperManage(paperId, 2, fillNum);
            int index = paperService.add(paperManage);
            if (index == 0)
                return RespDataHandler.buildRespData(400, "填空题题组卷保存失败", null);
        }
        //从数据库随机获取判断题
        List<Integer> judges = judgeQuestionService.findBySubject(item.getSubject(), judgeNumber);
        if (fills == null)
            return RespDataHandler.buildRespData(400, "判断题数据库获取失败", null);
        for (Integer judge : judges) {
            PaperManage paperManage = new PaperManage(paperId, 3, judge);
            int index = paperService.add(paperManage);
            if (index == 0)
                return RespDataHandler.buildRespData(400, "判断题题组卷保存失败", null);
        }
        return RespDataHandler.buildRespData(200, "试卷组卷成功", null);
    }
}
